package com.winbomb.kingcard.base.anim.impl;

import com.winbomb.kingcard.GameMsg;
import com.winbomb.kingcard.ICard;
import com.winbomb.kingcard.IGameLogic;
import com.winbomb.kingcard.IPlayer;
import com.winbomb.kingcard.NoMoreCardException;
import com.winbomb.kingcard.Pos;
import com.winbomb.kingcard.base.GameState;
import com.winbomb.kingcard.base.anim.IPlayable;
import com.winbomb.kingcard.base.anim.TransAnimation;
import com.winbomb.kingcard.screens.util.Render;

/**
 * 发牌的动画
 * 
 * @author winbomb
 * 
 */
public class DealingCardAnim implements IPlayable {

	private TransAnimation transAnim;
	private IGameLogic game;
	private IPlayer tgPlayer;
	private ICard topCard;
	private Pos tgPos;

	public DealingCardAnim(IGameLogic game) {
		this.game = game;
		transAnim = new TransAnimation(0, 0, 0, 0);
	}

	public boolean play(float dt) {
		if (!transAnim.hasFinished()) {
			if (!tgPlayer.isHuman()) {
				Render.drawCardBack((int) transAnim.getCurrX(), (int) transAnim.getCurrY());
			} else {
				Render.drawCard(topCard, (int) transAnim.getCurrX(), (int) transAnim.getCurrY(), true);
			}
			transAnim.step(dt);
			return false;
		}

		tgPlayer.drawCard();

		// 如果tgPlayer右边或者的player是currTurn的话,说明已经发了一圈了
		IPlayer nextPlayer = tgPlayer.getRightAlivePlayer();
		game.setCurrPlayer(nextPlayer);
		if (nextPlayer == game.getCurrTurn()) {
			GameMsg msg = new GameMsg(0);
			msg.addPlayer(nextPlayer.getPlayerName());
			msg.add(" 回合开始.");
			game.getGameRecorder().add(msg);
			game.setState(GameState.SELECT_ACTION);
		}

		return true;
	}

	public void setTarget(IPlayer tgPlayer) {
		this.tgPlayer = tgPlayer;
		this.tgPos = tgPlayer.getPlayerInfo().getFramePos();

		try {
			this.topCard = game.getCardPile().getTopCard();
		} catch (NoMoreCardException ex) {
			// TODO:
		}

		transAnim.clear();
		transAnim.setStart(215, 90);
		transAnim.setTarget(tgPos.x, tgPos.y);
		transAnim.setDuration(800f);
	}

	public void reset() {
		this.tgPlayer = null;
		this.tgPos = null;
	}
}
